package com.geyue.controller;

import com.geyue.common.toolbox.result.Res;
import com.geyue.core.model.Admin;
import com.geyue.core.model.Live;
import com.geyue.core.service.LiveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * Created by Lyon on 2017/4/24.
 */
@Controller
@RequestMapping("/live")
public class LiveController {

    @Autowired
    private LiveService liveService;

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String list(Model model,HttpSession session) {
        Admin admin = (Admin) session.getAttribute("admin");

        if(admin == null)
            return "error";

        return "live/list";
    }

    @RequestMapping(value = "/page", method = RequestMethod.POST)
    @ResponseBody
    public Res index(Live live,Integer pageSize, Integer pageNum,HttpSession session) {
        Admin admin = (Admin) session.getAttribute("admin");

        if(admin == null)
            return Res.fail("登录信息过时,请重新登录!");

        return Res.success("", liveService.getPage(pageNum, pageSize, live));
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    @ResponseBody
    public Res edit(@PathVariable Integer id, HttpSession session) {
        Admin admin = (Admin) session.getAttribute("admin");

        if(admin == null)
            return Res.fail("登录信息过时,请重新登录!");

        Live live = new Live();
        live.setId(id);
        live.setIsOpen(0);

        if(liveService.updateNotEmpty(live) > 0)
            return Res.success("修改成功!");

        return Res.fail("修改失败!");
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public String list(@PathVariable Integer id, Model model,HttpSession session) {
        Admin admin = (Admin) session.getAttribute("admin");

        if(admin == null)
            return "error";

        model.addAttribute("info",liveService.getOneByKey(id));
        return "live/info";
    }


}
